import { getBanners } from '@/api/banner'
import { url } from '@/urlConfig'
export default {
	namespaced: true,
	state: {
		loading: false,
		data: [],
	},
	mutations: {
		setLoading(state, payload) {
			state.loading = payload
		},
		setData(state, payload) {
			payload.forEach((item) => {
				if (!(item.midImg && item.midImg.startsWith(url)) || !(item.bigImg && item.bigImg.startsWith(url))) {
					item.midImg = url + item.midImg
					item.bigImg = url + item.bigImg
				}
			})
			// 后端缺陷所以更
			state.data = payload
		},
	},
	actions: {
		async fetchBanner(ctx) {
			if (ctx.state.data.length) {
				return
			}
			ctx.commit('setLoading', true)
			const res = await getBanners()
			ctx.commit('setData', res)
			ctx.commit('setLoading', false)
		},
	},
}
